<template>
  <!-- <map></map> -->
  <view style="height: 100vh; background-color: #f5f6fa">
    <view class="custom-bg">
      <view class="custom-header-bg"></view>
      <view class="position-view">
        <view class="moment-wrap">
          <up-form :model="formData" :rules="rules" ref="form">
            <up-form-item prop="description">
              <up-textarea v-model="formData.description" placeholder="请输入内容" border="none" height="100"></up-textarea>
            </up-form-item>
            <up-gap height="10"></up-gap>
            <view class="image-view">
              <view v-for="(item, index) in formData.fileUrl" :key="index" style="margin: 6rpx">
                <up-image :show-loading="true" :src="item" width="80px" height="80px" radius="10" @click="upload"></up-image>
              </view>
              <view class="upload-view" @click="upload" v-if="formData.fileUrl.length < 1">
                <up-icon name="camera-fill" size="20" color="#D3D4D6"></up-icon>
              </view>
            </view>
          </up-form>
        </view>
        <up-button text="提交发布" type="primary" @click="submit" shape="circle"></up-button>
      </view>
    </view>
  </view>
</template>

<script>
import { activityApi } from '@/api/activity/activity-app.js'
import { getToken } from '@/utils/auth'
import config from '@/config'

export default {
  data() {
    return {
      bg: 'https://jqgh-public.yqfoodec.com/20250411/bg-2_64874.jpg',
      fileList: [],
      value1: '',
      formData: {
        snapshotId: null,
        briskWalkId: null, // 健步走id
        riskWalkSiteId: null, // 健步走站点id
        walkFeatureId: null,
        description: '', // 描述
        fileUrl: [], // 作品地址数组
        publishAddress: '', // 发布地址
        publishLongitude: '', // 经度
        publishLatitude: '', // 纬度
      },
      options: {},
      rules: {
        description: {
          type: 'string',
          required: true,
          message: '请输入内容',
          trigger: ['blur', 'change'],
        },
      },
    }
  },
  onLoad(options) {
    //console.log(options) // walkFeatureId=='undefined' 单独随手拍
    const id = options.id
    this.options = options
    this.formData.briskWalkId = id
    this.formData.snapshotId = id
    this.formData.walkFeatureId = options.walkFeatureId
    // 获取当前地址
    // this.getLocationInfo();

    // this.$refs.PrivacyPop.checkPrivacySetting();
  },
  onReady() {
    //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
    this.$refs.form.setRules(this.rules)
  },
  methods: {
    // 获取位置信息
    getLocationInfo() {
      let that = this
      uni.getLocation({
        type: 'wgs84',
        geocode: true,
        success: function (res) {
          that.formData.publishLongitude = res.longitude
          that.formData.publishLatitude = res.latitude
          //腾讯地图逆地址解析--获取城市信息
          uni.request({
            url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=6GXBZ-EKOLQ-VY65W-275XQ-VNXR7-FMFWF`,
            success: (res) => {
              console.log(res) //位置信息 address_component
              that.formData.publishAddress = res.data.address
            },
          })
        },
        fail(err) {
          uni.showModal({
            title: '请求授权失败',
            content: '需要获取您的地理位置信息，请到设置中开启',
            showCancel: false,
          })
        },
      })
    },
    /**
     * 上传图片
     */
    upload() {
      let that = this
      uni.chooseImage({
        count: 9,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          console.log(res)
          // console.log(tempFilePaths);
          uni.showLoading({
            title: '正在上传...',
          })
          res.tempFilePaths.forEach((item) => {
            uni.uploadFile({
              url: config.baseUrl + '/sys/file/upload?access_token=' + getToken(),
              filePath: item,
              header: {
                'content-type': 'multipart/form-data',
              },
              name: 'file',
              success: function (checkres) {
                uni.hideLoading()
                let checkResult = JSON.parse(checkres.data)
                // console.log(checkResult)
                // wx.cloud.callFunction({
                //   name: 'ContentCheck',
                //   data: {
                //     img: checkResult.data.url,
                //   },
                //   success(res) {
                //     if (res.result.errCode === 87014) {
                //       uni.showModal({
                //         title: '图片存在敏感信息！',
                //         showCancel: false,
                //       })
                //       return false
                //     } else {
                //       that.formData.fileUrl.push(checkResult.data.url)
                //     }
                //   },
                // })
                that.formData.fileUrl.push(checkResult.data.url)
              },
            })
          })
        },
      })
    },
    async submit() {
      let _this = this
      this.$refs.form
        .validate()
        .then((res) => {
          // wx.cloud.callFunction({
          //   name: 'ContentCheck',
          //   data: {
          //     msg: _this.formData.description,
          //   },
          //   success(res) {
          //     if (res.result.errCode === 87014) {
          //       uni.showModal({
          //         title: '您输入的内容违规！',
          //         showCancel: false,
          //       })
          //       return false
          //     } else {
          _this.submitApi()
          //     }
          //   },
          // })
        })
        .catch((error) => {
          console.log('error:', error)
        })
    },
    async submitApi() {
      const params = JSON.parse(JSON.stringify(this.formData))

      if (this.options.walkFeatureId == 'undefined') {
        delete params.briskWalkId
        delete params.riskWalkSiteId
        delete params.walkFeatureId
      } else {
        delete params.snapshotId
      }

      const result = this.options.walkFeatureId == 'undefined' ? await activityApi.getSnapshotAdd(params) : await activityApi.snapshotAdd(params)

      if (result.data) {
        uni.showModal({
          title: '提示',
          content: `恭喜您获取了${result.data}个积分`,
          confirmText: '确定',
          success: function (res) {
            resolve(res)
          },
        })
      } else {
        uni.showModal({
          title: '发布成功！',
          showCancel: false,
        })
      }

      uni.navigateBack({
        delta: 1,
      })
    },
  },
}
</script>

<style lang="scss" scoped>
map {
  width: 100%;
  height: 200px;
}
.moment-wrap {
  min-height: 464rpx;
  border-radius: 32rpx;
  background-color: #fff;
  padding: 30rpx;
  margin-bottom: 40rpx;
  .name {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 32rpx;
    font-weight: bold;
  }
}

.image-view {
  display: flex;
  flex-wrap: wrap;
}
.upload-view {
  background-color: #f2f2f2;
  border-radius: 10rpx;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 6rpx;
}
</style>
